unit Unit25; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, sqldb, db,
  Menus, DBGrids, Unit1, Unit26, Unit22, Unit28, DateUtils, LR_DBSet, LR_Class;

type

  { TForm25 }

  TForm25 = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    frDBDataSet1: TfrDBDataSet;
    frReport1: TfrReport;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem11: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow;
    procedure frReport1GetValue(const ParName: String; var ParValue: Variant);
    procedure MenuItem10Click(Sender: TObject);
    procedure ordenar_grid();
    procedure MenuItem11Click(Sender: TObject);
    procedure MenuItem3Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem7Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
  private
    fecha_desde : string;
    fecha_hasta : string;
    proveedor : string;
  public
    { public declarations }
  end; 

var
  Form25: TForm25; 

implementation

{ TForm25 }

procedure TForm25.ordenar_grid();
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  SQLQuery1.SQL.Text := 'select * from vw_compra where cmp_fecha >= ''' + fecha_desde
    + ''' and cmp_fecha <= ''' + fecha_hasta + ''' and prv_nombre like '''
    + proveedor + '''order by cmp_fecha, cmp_cdg';

  SQLQuery1.Open;
  DBGrid1.Columns[0].Title.Caption := 'Fecha';
  DBGrid1.Columns[1].Title.Caption := 'Código';
  DBGrid1.Columns[2].Title.Caption := 'Producto';
  DBGrid1.Columns[3].Title.Caption := 'Proveedor';
  DBGrid1.Columns[4].Title.Caption := 'Cantidad';

  DBGrid1.Columns[0].Width := 80;
  DBGrid1.Columns[1].Width := 60;
  DBGrid1.Columns[2].Width := 200;
  DBGrid1.Columns[3].Width := 200;
  DBGrid1.Columns[4].Width := 80;

  DBGrid1.Columns[5].Visible :=  false;
end;

procedure TForm25.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;

  fecha_desde := DateToStr(StartOfTheMonth(Today));
  fecha_hasta := DateToStr(EndOfTheMonth(Today));
  proveedor := '%';
  
  ordenar_grid;
end;

procedure TForm25.frReport1GetValue(const ParName: String; var ParValue: Variant
  );
begin
  if ParName = 'proveedor' then
    ParValue := proveedor;
    
  if ParName = 'fecha_desde' then
    ParValue := fecha_desde;
    
  if ParName = 'fecha_hasta' then
    ParValue := fecha_hasta;
end;

procedure TForm25.MenuItem10Click(Sender: TObject);
begin
  frReport1.LoadFromFile('reporte_compras.lrf');
  frReport1.ShowReport;
end;

procedure TForm25.MenuItem11Click(Sender: TObject);
begin
  FormShow;
end;

procedure TForm25.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm25.MenuItem3Click(Sender: TObject);
begin
  Close;
end;

procedure TForm25.MenuItem4Click(Sender: TObject);
begin
  Form26.ShowModal;
  FormShow;
end;

procedure TForm25.MenuItem5Click(Sender: TObject);
var
  codigo : string;
  QueryString : string;
  
begin
  codigo := InputBox('Anular Compra', 'Ingrese el código de compra a anular:', '');
  
  if codigo <> '' then
  begin
    QueryString := 'delete from compra where cmp_cdg = ' + codigo;
    
    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    ShowMessage('BAJA EXITOSA');
    FormShow;
  end;
end;

procedure TForm25.MenuItem7Click(Sender: TObject);
begin
  Form22.ShowModal;
  
  fecha_desde := Form22.fecha_desde;
  fecha_hasta := Form22.fecha_hasta;
  
  ordenar_grid;
end;

procedure TForm25.MenuItem8Click(Sender: TObject);
begin
  Form28.ShowModal;
  
  proveedor := Form28.proveedor;
  
  if proveedor <> '' then
    ordenar_grid
  else
    proveedor := '%';
end;

initialization
  {$I unit25.lrs}

end.

